package cn.outsourcing.supermarket.service.impl;

import cn.outsourcing.supermarket.common.constants.CommonConstants;
import cn.outsourcing.supermarket.common.utils.DataUtils;
import cn.outsourcing.supermarket.common.utils.JwtUtils;
import cn.outsourcing.supermarket.converter.HiolabsAdConverter;
import cn.outsourcing.supermarket.converter.HiolabsCategoryConverter;
import cn.outsourcing.supermarket.converter.HiolabsGoodsConverter;
import cn.outsourcing.supermarket.mapper.*;
import cn.outsourcing.supermarket.po.*;
import cn.outsourcing.supermarket.service.WxHomeService;
import cn.outsourcing.supermarket.vo.WxHomeInfoVO;
import cn.outsourcing.supermarket.vo.home.HomeCategoryListVO;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author gzkemays
 * @since 2021/10/22 0:29
 */
@Service
public class WxHomeServiceImpl implements WxHomeService {
  /** 分类数据服务 */
  @Resource private HiolabsCategoryMapper categoryMapper;
  /** 广告位服务 */
  @Resource private HiolabsAdMapper adMapper;
  /** 商品服务 */
  @Resource private HiolabsGoodsMapper goodsMapper;
  /** 公告服务 */
  @Resource private HiolabsNoticeMapper noticeMapper;
  /** 购物车服务 */
  @Resource private HiolabsCartMapper cartMapper;

  @Resource private HttpServletRequest request;

  @Override
  public WxHomeInfoVO getHomeInfo() {
    try {
      long today = DataUtils.getSeconds();
      // 获得可以在首页展示的分类
      List<HiolabsCategory> categories =
          categoryMapper.selectList(
              Wrappers.<HiolabsCategory>lambdaQuery().eq(HiolabsCategory::getIsShow, 1));
      List<HomeCategoryListVO> categoryList = new ArrayList<>();
      // 循环可用分类，并找出其商品
      for (HiolabsCategory category : categories) {
        Integer categoryId = category.getId();
        HomeCategoryListVO wxHomeCategoryListVO =
            HiolabsGoodsConverter.INSTANCE.convertToHomeGoodsList(category);
        // 取前6个数据,根据 order sort 降序
        wxHomeCategoryListVO.setGoodsList(
            HiolabsGoodsConverter.INSTANCE.convertToHomeGoods(
                goodsMapper.selectList(
                    Wrappers.<HiolabsGoods>lambdaQuery()
                        .eq(HiolabsGoods::getCategoryId, categoryId)
                        .eq(HiolabsGoods::getIsDelete, 0)
                        .eq(HiolabsGoods::getIsOnSale, 1)
                        .orderByAsc(HiolabsGoods::getSortOrder)
                        .last("limit 6"))));
        categoryList.add(wxHomeCategoryListVO);
      }
      long cartCount = 0;
      String token = request.getHeader(CommonConstants.TOKEN_HEADER_KEY);
      if (token != null && !StringUtils.isBlank(token)) {
        Integer userId =
            (Integer) JwtUtils.getTokenClaimByRequest(request, CommonConstants.TOKEN_USER_ID_KEY);
        cartCount =
            cartMapper
                .selectList(
                    Wrappers.<HiolabsCart>lambdaQuery()
                        .eq(HiolabsCart::getIsDelete, 0)
                        .eq(HiolabsCart::getUserId, userId))
                .stream()
                .collect(Collectors.summarizingInt(HiolabsCart::getNumber))
                .getSum();
      }
      return WxHomeInfoVO.builder()
          .cartCount(cartCount)
          .channel(
              HiolabsCategoryConverter.INSTANCE.converterToChannelList(
                  categoryMapper.selectList(
                      Wrappers.<HiolabsCategory>lambdaQuery().eq(HiolabsCategory::getIsShow, 1))))
          .banner(
              HiolabsAdConverter.INSTANCE.converterToBanner(
                  adMapper.selectList(
                      Wrappers.<HiolabsAd>lambdaQuery()
                          .gt(HiolabsAd::getEndTime, today)
                          .eq(HiolabsAd::getIsDelete, 0))))
          .notice(
              noticeMapper.selectList(
                  Wrappers.<HiolabsNotice>lambdaQuery()
                      .gt(HiolabsNotice::getEndTime, today)
                      .eq(HiolabsNotice::getIsDelete, 0)))
          .categoryList(categoryList)
          .build();
    } catch (Exception e) {
      e.printStackTrace();
    }
    return null;
  }
}
